//
//  SearchController.m
//  FinancialProduct
//
//  Created by Stella on 2017/9/4.
//  Copyright © 2017年 maq. All rights reserved.
//

#import "UIAlertView+ActionBlock.h"
#import "CategoryCell.h"
#import "SearchController.h"

@interface SearchController ()
@property (weak, nonatomic) IBOutlet UIView *headerView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tableTop;
@property (weak, nonatomic) IBOutlet UITextField *searchTF;
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *mTableHeight;
@property (strong, nonatomic) NSMutableArray *SearchNameArrary;


@end

@implementation SearchController
- (IBAction)search:(id)sender {
    if (self.searchTF.text.length==0) {
        [self.navigationController popViewControllerAnimated:YES];
        return;
    }
    self.returnSearchContent(self.searchTF.text);
    [self uploadSearchName:self.searchTF.text];
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)uploadSearchName:(NSString *)name{
    //保存记录到本地
    if (name.length==0) {
        return;
    }
    NSMutableArray *recordArray=[NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults]objectForKey:@"RecordList"]];
    if ([recordArray containsObject:name]) {
        [recordArray removeObject:name];
    }
    [recordArray insertObject:name atIndex:0];
    [[NSUserDefaults standardUserDefaults] setObject:recordArray forKey:@"RecordList"];
    _SearchNameArrary=recordArray;
    self.mTableHeight.constant=_SearchNameArrary.count*44+60;
    [self.myTableView reloadData];
    
    
}
- (IBAction)remove:(id)sender {
    NSString *msg = @"您确定删除历史搜索记录？";
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:msg delegate:self cancelButtonTitle:@"否" otherButtonTitles:@"是",nil];
    alert.callBack = ^(UIAlertView *alertView, NSUInteger buttonIndex){
        if (buttonIndex == 1) {
            //清空

            [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"RecordList"];
            [_SearchNameArrary removeAllObjects];
            self.mTableHeight.constant=44;
            [self.myTableView reloadData];
        }

    };
    [alert show];
    
  
}
- (IBAction)searchCategory:(UIButton *)sender {
    self.returnCategory(sender.tag);
//    [self uploadSearchName:sender.titleLabel.text];
     [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)chooseMoneyType:(UIButton *)sender {
    
    self.returnMoneyType(sender.titleLabel.text);
//    [self uploadSearchName:sender.titleLabel.text];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"产品搜索";
    self.tableTop.constant=250;
    self.headerView.hidden=NO;
    if ([self.whereFrom isEqualToString:@"Report"]) {
        self.title=@"搜索";
        self.tableTop.constant=0;
        self.headerView.hidden=YES;
    }
    _SearchNameArrary=[[NSMutableArray alloc] initWithArray:[[NSUserDefaults standardUserDefaults]objectForKey:@"RecordList"]];
    self.mTableHeight.constant=_SearchNameArrary.count*44+60;
    [self setTextfile:self.searchTF img:[UIImage imageNamed:@"sousuo"]];
    // Do any additional setup after loading the view.
    [self.searchTF becomeFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)setTextfile:(UITextField *)textfile img:(UIImage *)img{
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    imageView.image =img;
    imageView.contentMode = UIViewContentModeCenter;
    textfile.leftView = imageView;
    textfile.leftViewMode = UITextFieldViewModeAlways;
    textfile.layer.cornerRadius = 5;
}

#pragma mark - Table view data source
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.SearchNameArrary.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CategoryCell *cell=[tableView dequeueReusableCellWithIdentifier:@"CategoryCell" forIndexPath:indexPath];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.accessoryType=UITableViewCellAccessoryNone;
    cell.NameLabel.text=self.SearchNameArrary[indexPath.row];
    return cell;
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *text=self.SearchNameArrary[indexPath.row];
    NSArray *textAaa=@[@"现金管理",@"固收/类固收",@"一级市场",@"二级市场"];
    if ([textAaa containsObject:text]) {
//        self.returnCategory(text);
    }
    self.returnSearchContent(text);
    [self uploadSearchName:text];
    [self.navigationController popViewControllerAnimated:YES];
}
@end
